﻿using Ec.NETCoreFramework.Common;
using Ec.NETCoreFramework.Common.MQ;
using Ec.NETCoreFramework.Common.Core.Utilities;
using Ec.NETCoreFramework.Common.Core.Serializer;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Ec.NETCoreFramework.Common.Plug.Core;
using Ec.NETCoreFramework.Common.Core;

namespace MQ_WechatCustomContact
{
    /// <summary>
    /// 根据手机号获取user
    /// </summary>
    public class CustomContact : TaskInterface
    {
        private ConsumeConfig _consumeConfig;
        public override async Task ConsumeEvent(byte[] bytes)
        {
            var dic = ByteSerializer.Deserialize<Dictionary<string, object>>(bytes);

            if (dic == null || !dic.ContainsKey("fk_id"))
            {
                Console.WriteLine("对象为空");
                return;
            }
            _consumeConfig.FKId = Utils.ToInt(dic["fk_id"]);
            _consumeConfig.DBId = Utils.ToInt(dic["db_id"]);
            var actoin = Utils.ToString(dic["action"]);
            ICustomContact bll = null;
            var appSecret = new RequestAppSecret()
            {
                AppId = _consumeConfig.AppId,
                Secret = _consumeConfig.Secret
            };
            if (actoin == CustomContactEnum.MobileToUser.ToString())
            {
                bll = new MobileToUser(appSecret);
            }
            else if (actoin == CustomContactEnum.ExternalContact.ToString())
            {
                bll = new ExternalContact(appSecret);
            }
            else if (actoin == CustomContactEnum.GroupChat.ToString())
            {
                bll = new GroupChat(appSecret);
            }
            if (bll != null)
            {
                await bll.Excute(_consumeConfig);
            }
        }
        public override string QueueName()
        {
            return "wechat-" + _consumeConfig.MQName;
        }
        public override void SetConsumeConfig(byte[] consumeConfig)
        {
            _consumeConfig = ProtoBufSerializer.DeSerialize<ConsumeConfig>(consumeConfig);
        }
    }
}
